package com.glitech.demo;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.text.HtmlCompat;



public class MainActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        initView();
        SharedPreferences mSharedPreferences = getSharedPreferences("name", Activity.MODE_PRIVATE);
        CmdViewModel mCmdViewModel = new CmdViewModel(getApplication());

    }

    private Intent intent;

    private void isPrivacyConfirm(Intent i) {
        intent = i;
        mPermissionResult.launch(Manifest.permission.RECORD_AUDIO);
    }

    private void initView() {
        TextView tipTv = (TextView) findViewById(R.id.tip);
        String buf = "离线 Demo\n" +
                "当前APPID为：" +
                getString(R.string.app_id) + "\n";
        tipTv.setText(buf);
        // 语法识别
        findViewById(R.id.asrBtn).setOnClickListener(v -> {
            isPrivacyConfirm(new Intent(MainActivity.this, AsrDemo.class));
        });
    }

    private Toast mToast;

    private void showTip(final String str) {
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT);
        mToast.show();
    }

    private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
            new ActivityResultContracts.RequestPermission(),
            result -> {
                if (result) {
                    if (intent !=null){
                        MainActivity.this.startActivity(intent);
                    }
                    SpeechApp.initializeMsc(MainActivity.this);
                }
            });
}
